export const CanonWebScanmgrCtrl=($scope, $window, $timeout, $routeParams, $location,
		dbService, enumService, $modal, $q, $http,$upload)=>{

	dbService.findById("nav",$routeParams.nav6id,function(result){

    	$scope.nav6name = result.results[0].name;
    	
    });
	
	
/*	 $scope.importcard = function() {
		var itemList = document.getElementsByClassName('importcardimage') // 所有card
		var index = 0
		for(var i = 0; i<itemList.length; i++){ 
		　　console.log(itemList.item(i).src); 
		$http.post(ctx + "/filemanager/decryptByBase64", {
			base64str : itemList.item(i).src,
			title : itemList.item(i).title
			//bln :itemList.item(i).bln
		}).success(function(result) {
		});
		}
		$http.post(ctx + "/filemanager/releaseCache", {
			groupId :$routeParams.groupId,
			nav6id : $routeParams.nav6id
		}).success(function(result) {
			 $location.path("/itemCasemgr").search({
				 groupId: $routeParams.groupId,
					nav6id :$routeParams.nav6id
				})
		});
		
	 }
	 
	 $scope.importcard21 = function() {
			var successCount=0;
			var itemList = document.getElementsByClassName('importcardimage') // 所有card
			var index = 0
			for(var i = 0; i<itemList.length; i++){ 
			      $scope.upload = $upload.upload({
			        url: ctx + '/filemanager/decryptByBase64',
			        data: {'base64str': itemList.item(i).src,'title':itemList.item(i).title},
			      }).progress(function(evt) {
			      }).success(function(data, status, headers, config) {
			    	  successCount++;
			    	  if(successCount==itemList.length)
		    		  {
			    		$http.post(ctx + "/filemanager/releaseCache", {
			    			groupId :$routeParams.groupId,
			    			nav6id : $routeParams.nav6id
			    		}).success(function(result) {
			    			 $location.path("/itemCasemgr").search({
			    				 groupId: $routeParams.groupId,
			    					nav6id :$routeParams.nav6id
			    				})
			    		});
		    		  }
			      });
			    } 
		};*/
		
		 $scope.importcard2 = function() {
				var successCount=0;
				var itemList = document.getElementsByClassName('importcardimage') // 所有card
				var listarr; 
				var index = 0
				for(var i = 0; i<itemList.length; i++){ 
					listarr=listarr+itemList.item(i).src;
				}
				      $scope.upload = $upload.upload({
				        url: ctx + '/filemanager/decryptByBase642',
				        data: {'groupId':$routeParams.groupId,'nav6id':$routeParams.nav6id,'itemList':listarr},
				      }).progress(function(evt) {
				      }).success(function(data, status, headers, config) {
				    	  $location.path("/itemCasemgr").search({
			    				 groupId: $routeParams.groupId,
			    					nav6id :$routeParams.nav6id
			    				})
				      });
			};
		 $scope.importcard3 = function() {
				
				    			 $location.path("/itemCasemgr").search({
				    				 groupId: $routeParams.groupId,
				    					nav6id :$routeParams.nav6id
				    				})
				 
			};
	 
			$scope.fileSelected = function($files) {
			    //$files: an array of files selected, each file has name, size, and type.
			var dateflag=Date.now();
			$scope.dateflag=dateflag;
			var successCount=0;
			for (var i = 0; i < $files.length; i++) {
			      var file = $files[i];

			      var filename= file.name;
			      $scope.upload = $upload.upload({
			        url: ctx + '/filemanager/upload9',
			        //method: 'POST' or 'PUT',
			        //headers: {'header-key': 'header-value'},
			        //withCredentials: true,
			        data: {'requireFiletype': 'jpg,jpeg,png','dir':'file','dateflag':dateflag,'filename':filename},
			        fileFormDataName: 'imgFile',
			        file: file, // or list of files ($files) for html5 only
			      }).success(function (results) {
			    	  successCount++;
			    	  if(successCount==$files.length)
		    		  {
			    		  dbService.findAll("cardtmp","dateflag = ? order by filename", [dateflag], function(result) {
			    			  var cardtmps = result.results;
			    				for (var i = 0; i < cardtmps.length; i++) {
			    					 var cardtmp = cardtmps[i];
			    					 appendImage(cardtmp.url,i+1);
			    				}
			    				if((cardtmps.length%2)==0){//判定条件余数为0时为偶数
			    					alert("本次上传"+cardtmps.length/2+"张答题卡")
			    				 }else{
			    					 alert("本次上传"+cardtmps.length/2+"张答题卡,图片数量错误,建议刷新后重新上传！")
			    				 }
			    				
			    			});
		    			 
		    			  
		    		  }
					
			    });
			    } 
		};		
			
		
		 $scope.importcard4 = function() {
				$http.post(ctx + "/filemanager/releaseCache2", {
	    			groupId :$routeParams.groupId,
	    			nav6id : $routeParams.nav6id,
	    			dateflag : $scope.dateflag
	    		}).success(function(result) {
	    			 $location.path("/itemCasemgr").search({
	    				 groupId: $routeParams.groupId,
	    					nav6id :$routeParams.nav6id
	    				})
	    		});
			};
			
	 function appendImage(url,i) {
     	var imagediv = document.getElementById("imagediv");
             var item = document.createElement("img");
             item.setAttribute('src',url);
		/*item.setAttribute('width','300px');
		item.setAttribute('height','300px');*/
        item.setAttribute('style','width:300px;height:300px;');
        if(i%2 == 0){
			item.setAttribute('title',"第"+(i)/2+"张反面");
			//item.setAttribute('num',(i)/2);
			//item.setAttribute('bln',1);
			}else{
			item.setAttribute('title',"第"+(i+1)/2+"张正面");
			//item.setAttribute('bln',0);
			}
		
		item.setAttribute('class',"importcardimage");
             

             var doScroll = imagediv.scrollTop > imagediv.scrollHeight - imagediv.clientHeight - 1;
             imagediv.appendChild(item);
             if (doScroll) {
                 imagediv.scrollTop = imagediv.scrollHeight - imagediv.clientHeight;
             }
     }
	
	 
	 //$scope.Scan = function Scan() { 
		 $scope.Scan = function() {
	    var btnScan = document.getElementById("btnScan");;
         var socket;
	    var strmsg="连接Canon扫描仪网页服务程序失败!";
	    var i=0;
	
	    btnScan.disabled = true;
         
	    socket = new WebSocket("ws://127.0.0.1:18596/");
         try {
             socket.onclose = function (msg){ if(strmsg!=""){alert(strmsg)};btnScan.disabled = false};
             
		socket.onopen = function (msg) {
			
		    strmsg="连接异常中断";	    
		    var command = { command: "scan", pixeltype: 2, resolution: 200,  duplex: 2 };
		    socket.send(JSON.stringify(command));}

             socket.onmessage = function (msg) {
            	
                 if (typeof msg.data == "string") {      
						strmsg="";
					/*	if(i%2 == 0){
							alert(i + "图片是偶数");
							if(i!=0){
								alert("本次上传"+(i+1)/2+"张答题卡");
							}
							}else{
							alert(i +"图片是奇数");
							}*/
						if(i!=0){
							$timeout(function(){
								if((i%2)===0){//判定条件余数为0时为偶数
			    					alert("本次上传"+i/2+"张答题卡")
			    				 }else{
			    					 alert("本次上传"+i/2+"张答题卡,图片数量错误,建议刷新后重新上传！")
			    				 }
							}, 1000*1);
						}
						
						//alert(JSON.stringify(JSON.parse(msg.data)));
				    }
				    else
				    {
						var reader = new FileReader();
						reader.onload = function(msg)
						{ i++;
							if(msg.target.readyState == FileReader.DONE)
							{
								var url = msg.target.result;
								appendImage(url,i);
							}
						}		
						reader.readAsDataURL(msg.data);           
				    }
             }

         }
         catch (ex) {
             log(ex);
         }
     }	
}

// CanonWebScanmgrCtrl.$inject = [ '$scope', '$window', '$timeout', '$routeParams',
// 		'$location', 'dbService', 'enumService', '$modal', '$q', '$http','$upload' ];